# coding:utf8
import csv
import json

import chardet
import yaml
import pandas as pd
from pathlib import Path
import pytest
from xToolkit import xfile

def get_encoding(file_path):
    with open(file_path,'rb') as file:
        reder = file.read()
        encoding = chardet.detect(reder)['encoding']
        return encoding

def read_csv_data(file_path):
    """
    读取CSV文件数据，返回列表形式的数据
    :param file_path: CSV文件的路径
    :return: 包含CSV文件每一行数据的列表，每行数据以字典形式表示（第一行为表头作为字典的键）
    """
    data = []
    with open(file_path,'rb') as file:
        raw_data = file.read()
        encoding = chardet.detect(raw_data)['encoding'] #检测csv文件的编码
    with open(file_path, 'r', encoding=encoding) as f:
        reader = csv.DictReader(f)
        for row in reader:
            data.append(row)
    return data


def read_yaml_data(file_path):
    """
    读取yaml文件数据
    :param file_path: yaml文件的路径
    :return: 解析后的yaml数据（通常是字典或者列表形式）
    """
    config_path = Path(file_path)
    with config_path.open('r') as f:
        return yaml.safe_load(f)
def get_excel_data(file_path):
    datas = xfile.read(file_path).excel_to_dict(sheet=0)
    return datas
#读取excel文件
def get_Excel_data(file_path):
    df = pd.read_excel(file_path)
    return df
if __name__ == '__main__':
    a = get_excel_data('D:\\ruanjian\pythonProject\\api_test_project\\api_test\data\\模块接口测试用例.xls')
    b = json.dumps(a[0]['请求头'])
    print(b)